package com.oracle.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.pojo.Teacher;
import com.oracle.service.ITeacherService;
import com.oracle.util.MD5;
import com.oracle.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "教师信息控制器")
@RequestMapping("/sms/teacherController")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    /**
     * 模糊查询带分页操作
     */
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    @ApiOperation("模糊查询带分页功能")
    public Result getTeachersByConditions(
            @ApiParam("页码") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("页大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("模糊查询筛选条件")Teacher teacher
            ){
        Page<Teacher> teacherPage = new Page<>(pageNo, pageSize);
        IPage<Teacher> ipage = teacherService.getTeachersByConditions(teacherPage,teacher);
        return Result.ok(ipage);
    }

    /**
     * 添加教师信息或者修改教师信息
     */
    @PostMapping("/saveOrUpdateTeacher")
    @ApiOperation("添加教师信息或者修改教师信息")
    public Result saveOrUpdateTeacher(
            @ApiParam("修改或添加的教师对象") @RequestBody Teacher teacher
    ){
        Long id = teacher.getId();
        if(id == null || id == 0){
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }

        teacherService.saveOrUpdate(teacher);

        return Result.ok();
    }

    /**
     * 删除单条记录或者批量删除
     */
    @DeleteMapping("/deleteTeacher")
    @ApiOperation("删除一个教师信息或者批量删除教师信息")
    public Result deleteTeacherByIds(
            @ApiParam("被删除的教师的id数组") @RequestBody List<Integer> ids
            ){
        teacherService.removeByIds(ids);
        return Result.ok();
    }


}
